/*     */ package com.zimbra.cs.util.yauth;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import com.zimbra.cs.mailbox.MetadataList;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import java.io.IOException;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class MetadataTokenStore
/*     */   extends TokenStore
/*     */ {
/*     */   private final Mailbox mbox;
/*     */   private final Map<String, String> tokens;
/*  35 */   private static final Log LOG = ZimbraLog.datasource;
/*     */   private static final String YAUTH_KEY = "YAUTH";
/*     */   private static final String VERSION_KEY = "VERSION";
/*     */   private static final String TOKENS_KEY = "TOKENS";
/*     */   private static final long VERSION = 2L;
/*     */   
/*     */   public static void clearTokens(Mailbox mbox)
/*     */     throws ServiceException
/*     */   {
/*  44 */     mbox.setConfig(new OperationContext(mbox), "YAUTH", null);
/*     */   }
/*     */   
/*     */   public static void copyTokens(Mailbox from, Mailbox to) throws ServiceException {
/*  48 */     Metadata md = from.getConfig(new OperationContext(from), "YAUTH");
/*  49 */     if (md != null) {
/*  50 */       LOG.debug("Copying auth tokens from mailbox '%s' to '%s'", new Object[] { Integer.valueOf(from.getId()), Integer.valueOf(to.getId()) });
/*  51 */       to.setConfig(new OperationContext(to), "YAUTH", md);
/*     */     }
/*     */   }
/*     */   
/*     */   public MetadataTokenStore(Mailbox mbox) throws ServiceException {
/*  56 */     this.mbox = mbox;
/*  57 */     this.tokens = new HashMap();
/*  58 */     loadTokens();
/*     */   }
/*     */   
/*     */   public String newToken(String appId, String user, String pass) throws AuthenticationException, IOException
/*     */   {
/*  63 */     LOG.debug("Generating new token for user '%s'", new Object[] { user });
/*  64 */     return super.newToken(appId, user, pass);
/*     */   }
/*     */   
/*     */   /* Error */
/*     */   public String getToken(String appId, String user)
/*     */   {
/*     */     // Byte code:
/*     */     //   0: aload_0
/*     */     //   1: dup
/*     */     //   2: astore_3
/*     */     //   3: monitorenter
/*     */     //   4: aload_0
/*     */     //   5: getfield 16	com/zimbra/cs/util/yauth/MetadataTokenStore:tokens	Ljava/util/Map;
/*     */     //   8: aload_1
/*     */     //   9: aload_2
/*     */     //   10: invokestatic 20	com/zimbra/cs/util/yauth/MetadataTokenStore:key	(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
/*     */     //   13: invokeinterface 21 2 0
/*     */     //   18: checkcast 22	java/lang/String
/*     */     //   21: aload_3
/*     */     //   22: monitorexit
/*     */     //   23: areturn
/*     */     //   24: astore 4
/*     */     //   26: aload_3
/*     */     //   27: monitorexit
/*     */     //   28: aload 4
/*     */     //   30: athrow
/*     */     // Line number table:
/*     */     //   Java source line #69	-> byte code offset #0
/*     */     //   Java source line #70	-> byte code offset #4
/*     */     //   Java source line #71	-> byte code offset #24
/*     */     // Local variable table:
/*     */     //   start	length	slot	name	signature
/*     */     //   0	31	0	this	MetadataTokenStore
/*     */     //   0	31	1	appId	String
/*     */     //   0	31	2	user	String
/*     */     //   2	25	3	Ljava/lang/Object;	Object
/*     */     //   24	5	4	localObject1	Object
/*     */     // Exception table:
/*     */     //   from	to	target	type
/*     */     //   4	23	24	finally
/*     */     //   24	28	24	finally
/*     */   }
/*     */   
/*     */   public void putToken(String appId, String user, String token)
/*     */   {
/*  75 */     synchronized (this) {
/*  76 */       this.tokens.put(key(appId, user), token);
/*  77 */       saveTokens();
/*     */     }
/*     */   }
/*     */   
/*     */   public void removeToken(String appId, String user) {
/*  82 */     synchronized (this) {
/*  83 */       this.tokens.remove(key(appId, user));
/*     */     }
/*     */   }
/*     */   
/*     */   public int size() {
/*  88 */     return this.tokens.size();
/*     */   }
/*     */   
/*     */   private void loadTokens() throws ServiceException {
/*  92 */     Metadata md = this.mbox.getConfig(new OperationContext(this.mbox), "YAUTH");
/*  93 */     if (md != null) {
/*  94 */       long version = md.getLong("VERSION", 0L);
/*  95 */       if (version == 2L) {
/*  96 */         MetadataList ml = md.getList("TOKENS");
/*  97 */         if (ml != null) {
/*  98 */           loadTokens(ml);
/*     */         }
/*     */       }
/*     */     } else {
/* 102 */       LOG.debug("Creating new token store for mailbox '%s'", new Object[] { Integer.valueOf(this.mbox.getId()) });
/*     */     }
/*     */   }
/*     */   
/*     */   private void loadTokens(MetadataList ml) throws ServiceException {
/* 107 */     int size = ml.size();
/* 108 */     for (int i = 0; i < size; i++) {
/* 109 */       String[] parts = ml.get(i).split(" ");
/* 110 */       if (parts.length == 3) {
/* 111 */         String appId = parts[0];
/* 112 */         String user = parts[1];
/* 113 */         String token = parts[2];
/*     */         
/* 115 */         this.tokens.put(key(appId, user), token);
/*     */       }
/*     */     }
/* 118 */     LOG.debug("Loaded %d yauth token(s) for mailbox '%s'", new Object[] { Integer.valueOf(size), Integer.valueOf(this.mbox.getId()) });
/*     */   }
/*     */   
/*     */   private void saveTokens() {
/* 122 */     LOG.debug("Saving %d yauth token(s) for mailbox '%s'", new Object[] { Integer.valueOf(this.tokens.size()), Integer.valueOf(this.mbox.getId()) });
/*     */     try {
/* 124 */       Metadata md = new Metadata();
/* 125 */       md.put("VERSION", 2L);
/* 126 */       md.put("TOKENS", saveTokens(this.tokens));
/* 127 */       this.mbox.setConfig(new OperationContext(this.mbox), "YAUTH", md);
/*     */     } catch (ServiceException e) {
/* 129 */       throw new IllegalStateException("Unable to save tokens", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private MetadataList saveTokens(Map<String, String> tokens) {
/* 134 */     MetadataList ml = new MetadataList();
/* 135 */     for (Map.Entry<String, String> e : tokens.entrySet()) {
/* 136 */       ml.add((String)e.getKey() + " " + (String)e.getValue());
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 145 */     return ml;
/*     */   }
/*     */   
/*     */   private static String key(String appId, String user) {
/* 149 */     return appId + " " + user;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/yauth/MetadataTokenStore.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */